<template>  
  <div class="min-h serveProject bg-wt">
    <SearchForm
    :searchData="pagination"
    @handleClear="handleClear"
    @handleSearch="handleSearch"
    @handleReset="handleReset"
    />
    
    <TableList 
    :data="data" 
    :total="total"
    :pagination="pagination"
    @onCurrentChange="onCurrentChange"
    @onPageSizeChange="onPageSizeChange"
    @handleBulid="handleBulid"
    @handleUpdate="handleUpdate"
    @handleClickDelete="handleClickDelete"
    @handleForbidden="handleForbidden"
    />
  </div>
<!-- 删除弹层 -->
<Delete
    :visible="dialogDeleteVisible"
    :delete-text="operateText"
    @handleDelete="handleDelete"
    @handle-close="handleDeleteClose"
    ></Delete>

    <!-- 禁用弹层 -->
    <Forbidden
    :visible="dialogVisible"
    :text="operateText"
    @handle-submit="handleForbiddenSub"
    @handle-close="handleForbiddenClose"
    ></Forbidden>

  <DialogForm
      ref="dialogRef"
      :data="dataUp"
      :planData="planData"
      :visible="visible"
      :title="title"
      @handleClose="handleClose"
      @handleAdd="handleAdd"
      @handleEdit="handleEdit"
    >
    </DialogForm>
</template>

<script setup lang="jsx">
import { ref,onMounted  } from 'vue'
import { getLevelList,levelAdd,levelUpdate,getLevelDetails,
  levelStatus,getLevelAllList,getnursAllPlanList,levelDelete} from '@/api/serve'
import Delete  from '@/components/OperateDialog/index.vue'
import Forbidden from '@/components/Forbidden/index.vue'
import { MessagePlugin } from 'tdesign-vue-next'
import TableList  from './components/TableList.vue'
import SearchForm from './components/SearchForm.vue'
import DialogForm from '@/pages/serve/plan/grade/components/DialogFrom.vue'


const current = ref(1)
const data = ref([])
const dataUp =ref({})
const total = ref(0)
const pagination = ref({
  pageSize: 5,
  pageNum: 1,
  total,
  name,
  status,
});

//生命周期
onMounted(() => {
  getList()
  getPlanList()
})


const onPageSizeChange = (size) => {
  console.log('page-size:', size);
  pagination.value.pageSize=size
  MessagePlugin.success(`每页显示项目数变化为${size}`);
  getList()
};

const onCurrentChange = (index, pageInfo) => {
  MessagePlugin.success(`转到第${index}页`);
  console.log("参数是什么{}",pageInfo);
  pagination.value.pageNum=pageInfo.current
  getList()
};

// const onChange = (pageInfo) => {
//   console.log("干嘛啊{}",pageInfo);

//   // getList()
// };
//获取列表数据
const getList = async () => {
  const res = await getLevelList(pagination.value)
  data.value = res.data.records
  total.value = Number(res.data.total)
};

const handleClear=(val)=>{
  if(val === 'name'){
    delete pagination.value.name
  }else{
    delete pagination.value.status
  }
  getList()
}


const handleReset=()=>{
  
  pagination.value = {
    pageNum: 1,
    pageSize: 5
  }
  getList()
}

const handleSearch=()=>{
  pagination.value.pageNum = 1
  getList()
}




//是否显示弹窗
const title = ref('')
const visible = ref(false)
const dialogRef = ref(null)

//点击新增护理项目 按钮 把visible设置为true，弹出
const handleBulid = () =>{
  title.value='新增'
  visible.value = true;
}
//点击弹窗中的关闭或取消，关闭弹窗
const handleClose = () =>{
  visible.value = false;
}

//确认添加
const handleAdd=async(formData)=>{
    
    const result=await levelAdd(formData)
  console.log('新增护理等级',result);
    if(result.code === 200){
      MessagePlugin.success('新增成功')
      getList()
      dialogRef.value.handleClear()
    }else{
      MessagePlugin.error(result.msg)
    }
    
    handleClose();
    
}
//获取所有的护理计划
 const planData=ref([])
 const getPlanList=async()=>{
  const res = await getnursAllPlanList()
  planData.value = res.data
  console.log('护理等级列表',res.data);
}

//编辑弹窗
const handleUpdate=async(row)=>{
      title.value='编辑'
      visible.value = true;
      
      const result=await getLevelDetails(row.id);
      console.log('根据id查询护理等级信息',result);
      if(result.code == 200){
        MessagePlugin.success('获取信息成功')

        dataUp.value=result.data
       
      }else{
        MessagePlugin.error(result.msg)
      }
      
}

//编辑保存
const handleEdit=async(formData)=>{
  const result=await levelUpdate(formData)
  console.log('保存编辑的护理等级',result);
  if(result.code === 200){
    MessagePlugin.success('编辑成功')
    getList()
    dialogRef.value.handleClear()
  }else{
    MessagePlugin.error(result.msg)
  }
  handleClose();
}


const dialogDeleteVisible = ref(false) // 控制删除弹层显示隐藏
const dialogVisible = ref(false) // 禁用弹窗
const typeId = ref() // 设置删除id
const typeStatus = ref(null) // 禁用启用
const operateText = ref('护理等级') // 要操作的内容提示
const statusText = ref() // 启用禁用提示
// 点击删除弹窗
const handleClickDelete = (val) => {
  console.log('点击删除弹窗')
  typeId.value = val.id
  dialogDeleteVisible.value = true
}
// 禁用弹窗
const handleForbidden = (val) => {
  
  typeId.value = val.id
  if (val.status === 1) {
    dialogVisible.value = true
    typeStatus.value = 0
    statusText.value = '禁用成功'
  } else {
    typeStatus.value = 1
    handleForbiddenSub()
    statusText.value = '启用成功'
  }
}

// 关闭删除弹层
const handleDeleteClose = () => {
  dialogDeleteVisible.value = false
}
// 关闭禁用弹窗
const handleForbiddenClose = () => {
  dialogVisible.value = false
}

// 确定禁用
const handleForbiddenSub = async () => {
  const params = {
    id: typeId.value,
    status: typeStatus.value
  }
  const res = await levelStatus(params)//levelStatus
  if (res.code === 200) {
    dialogVisible.value = false
    MessagePlugin.success(statusText.value)
    getList()
  }
}

// 确认删除
const handleDelete = async () => {
  const res = await levelDelete(typeId.value)
  if (res.code === 200) {
    dialogDeleteVisible.value = false
    MessagePlugin.success('删除成功')
    getList() 
    console.log('成功删除的等级对应的id:{}',typeId.value)
  }
 
};


</script>